package com.sunmouse.toollib.widget;

import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.sunmouse.toollib.R;
import com.sunmouse.toollib.utils.anim.AnimUtil;


/**
 * Created by Kvin on 2017/6/7.
 */
@Deprecated
public class TipsDialog extends AlertDialog {
    private TextView mTitleTv;
    private TextView mDescTv;
    private Button cancelBtn;
    private Button confirmBtn;

    private View mContentView;


    public static TipsDialog mDialog;

    private TipsDialog(Context context) {
        super(context);
    }

    private TipsDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    private TipsDialog(Context context, int themeResId) {
        super(context, themeResId);

        init(context);
    }

    private void init(Context context) {
        this.mContentView = View.inflate(context, R.layout.dialog_tips, null);

        this.mTitleTv = (TextView) mContentView.findViewById(R.id.title_tv);
        this.mDescTv = (TextView) mContentView.findViewById(R.id.desc_tv);
        this.cancelBtn = (Button) mContentView.findViewById(R.id.cancel_btn);
        this.confirmBtn = (Button) mContentView.findViewById(R.id.confirm_btn);
    }

    /**
     *
     *
     */
    public static TipsDialog getTipsDialog(Context context) {
        if (mDialog == null) {
            mDialog = new TipsDialog(context, com.sunmouse.toollib.R.style.DialogUtil);
        }
        return mDialog;
    }

    /**
     * create a new dialog
     *
     * @param context
     * @return
     */
    public static TipsDialog newTipsDialog(Context context) {
        return new TipsDialog(context, com.sunmouse.toollib.R.style.DialogUtil);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setGravity(Gravity.CENTER);
        getWindow().setWindowAnimations(AnimUtil.Transition.FIFO);//set loading animation
        //set dialog size
        DisplayMetrics metrics = new DisplayMetrics();
        getWindow().getWindowManager().getDefaultDisplay().getMetrics(metrics);
        WindowManager.LayoutParams params = new WindowManager.LayoutParams();
        params.width = (int) (metrics.widthPixels );
        params.height = (int) (metrics.heightPixels );
        getWindow().setAttributes(params);
        if (mContentView != null)
            setContentView(mContentView);
    }

    @Override
    public void show() {
        if (!isShowing()) {
            super.show();
        }
    }


    @Override
    public void dismiss() {
        if (isShowing()) {
            super.dismiss();
        }
    }


    /**
     * set content
     */
    public void setTitle(String title) {
        this.mTitleTv.setText(TextUtils.isEmpty(title) ? "" : title);
    }

    public void setDesc(String desc) {
        this.mDescTv.setText(TextUtils.isEmpty(desc) ? "" : desc);
    }

    /**
     * set click event
     */
    public void setOnCancelClickListener(View.OnClickListener listener) {
        if (listener==null){
            throw new NullPointerException("listener is null");
        }
        this.cancelBtn.setOnClickListener(listener);
    }
    public void setOnConfirmClickListener(View.OnClickListener listener) {
        if (listener==null){
            throw new NullPointerException("listener is null");
        }
        this.confirmBtn.setOnClickListener(listener);
    }


    /**
     * set layout params
     */
    public void setLayoutParams(int w, int h) {
        if (mContentView != null) {
            mContentView.setLayoutParams(new ViewGroup.LayoutParams(w, h));
        }
    }

    public void setLayoutParams(ViewGroup.LayoutParams lp) {
        if (mContentView != null) {
            mContentView.setLayoutParams(lp);
        }
    }
}